//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')
var util = require('./utils/util.js')


App({
    onLaunch: function (callback) {
        //try {
        //    wx.clearStorageSync()
        //} catch(e) {  // Do something when catch error
        //}
        qcloud.setLoginUrl(config.service.loginUrl)
        this.login(callback);
        if (!util.isNUll(callback.scene) && (callback.scene == 1007 ||callback.scene == 1008)) { // 当用户通过带 shareTicket 的分享卡片进入小程序时，小程序才开始读取群聊信息
            if (!util.isNUll(callback.query.pid)) {
                wx.getShareInfo({
                    shareTicket: callback.shareTicket,
                    complete: function (res) {
                        wx.redirectTo({
                            //url: '/pages/share-link/index',
                            url: 'pages/share-link/index?pid=' + callback.query.pid + '&scene=' + callback.scene,
                        })
                    }
                });
            }
        }
        // 引入 SDK
        //require('./sdk-v<version>')
        // 初始化 SDK
        //let clientID = '知晓云管理后台获取到的 ClientID'
        //wx.BaaS.init(clientID)
    },
    login: function (callback) {
        var that = this;
        var userInfoStorage = wx.getStorageSync('userInfo');
        if (util.isNUll(userInfoStorage)||util.isNUll(userInfoStorage.userid)){
            util.showBusy('正在登录')

            qcloud.request({
                login: true,
                url: config.service.requestUrl,
                success: function (response) {
                    if(response.data.code==0){
                        that.getLogin(callback,response.data.data);
                    }
                },
                fail: function (err) {
                    console.log(err);
                }
            });

        //
        //    // 调用登录接口
        //    qcloud.login({
        //        success(result)
        //    {
        //        if (result) {
        //            qcloud.request({
        //                url: config.service.requestUrl,
        //                login: true,
        //                success(result)
        //            {
        //                that.getLogin(callback,result.data.data);
        //            }
        //        ,
        //            fail(error)
        //            {
        //                util.showModel('请求失败', error)
        //            }
        //        })
        //        } else {
        //            // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
        //            qcloud.request({
        //                url: config.service.requestUrl,
        //                login: true,
        //                success(result)
        //            {
        //                that.getLogin(callback,result.data.data);
        //            }
        //        ,
        //            fail(error)
        //            {
        //                util.showModel('请求失败', error)
        //                console.log('request fail', error)
        //            }
        //        })
        //
        //        }
        //    }
        //,
        //
        //    fail(error)
        //    {
        //        util.showModel('登录失败', error)
        //    }
        //})
        } else {
            that.globalData.userInfo = userInfoStorage
        }
    },
    getLogin: function (callback,param) {
        var that = this;
        wx.request({
            url: config.service.userLogin,
            data: {
                unionid: param.unionId,
                openid:param.openId,
                nickname:param.nickName,
                gender:param.gender,
                language:param.language,
                city:param.city,
                province:param.province,
                country:param.country,
                avatarurl:param.avatarUrl,
                skey:param.skey,
                timestamp:param.watermark.timestamp,
                appid:param.watermark.appid,
            },
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
                util.showSuccess('登录成功')
                if(res.data.data==1){
                    that.getLogin(callback,param)
                }else{
                    that.globalData.userInfo = res.data.data
                    wx.setStorage({
                        key: 'userInfo',
                        data: res.data.data,
                        success: function (res){

                            console.log('setStorage'+JSON.stringify(res));
                            typeof callback == "function" && callback(that.globalData.userInfo)
                        }
                        })
                }

            }
        });
    },

    globalData: {
        classifyList: {},  //商品列表
        userInfo: {},     //用户信息
        carts: [],       //购物车信息
        cartTotal: 0,     //购物车数量
        cartTotalPrice: 0,  //购物车价值
        cartAllIn: false,   //购物车是否全选
        appid:'wxda7ca4eac0e90af7',
        mch_id:'1497319732',
        access_token:'',
    },
})